<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <liujiqi@etrol.com> 
// +----------------------------------------------------------------------

namespace app\duty\controller;

use app\common\controller\Admin;
use app\duty\controller\api\News as NewsApi;
use app\admin\controller\ajax\Library as Ajax;

/**
 * 消息管理
 * @package app\News
 */
class News  extends Admin

{
    
    protected $api;
    
	public function _initialize()
	{
		parent::_initialize();
		$this->api = new NewsApi();
            $this->ajax= new Ajax();
	}
    /**
     * 首页
     * 
     */
   	public function index() {
   	  if(IS_AJAX){
   	  	$newsList = $this->api->newsList();

   	  	return $newsList;
   	  }
      $type=$this->ajax->getnewsType()->getData();
         
      $this->assign('type',$type);
      return $this->fetch();
 	}

 	/**
 	 * 消息
 	 */
 	public function search($start=null,$end=null,$keyword=null) {
 		if(IS_AJAX){
			$newsList = $this->api->search(input('start'),input('end'),input('keyword'));
 			return $newsList;
 		}
 	}

  /**
   * 消息详情
   */
  public function detail($newsId) {
        $newsList = $this->api->newsList(input('newsId'))->getData();
      $this->assign('info',$newsList['data'][0]); 
     
      return $this->fetch();
  }



    /**
     * 添加消息
     * 
     */
    public function add() {
   		 if(IS_POST){
   		 	$data = input('post.');  
   		 	//有效性
              if(isset($data['isValid'])){
                     $data['isValid'] = 1;
                     }else{
                       $data['isValid'] = 0;
                     }
              $data['sendTime']=time();
              $data['sendPersonId']=session('user_auth.id');
                  return $this->api->add($data);
   		 }
           $type=$this->ajax->getnewsType()->getData();
         
           $this->assign('type',$type);
          
           return $this->fetch();    
      }
 
    /**
     * 修改消息
     * 
     */
    public function edit($newsId = null) {
      
    	  if(IS_POST){
    	  	$data = input('post.');
                  //有效性
         	 if(isset($data['isValid'])){
                 $data['isValid'] = 1;
              }else{
                 $data['isValid'] = 0;
              }
              $data['sendTime']=time();
              $data['sendPersonId']=session('user_auth.id');
    	  	return $this->api->edit($data,$data['id']);
    	  }
     	 $newsList = $this->api->newsList($newsId)->getData();
         $type = $this->ajax->getnewsType()->getData();
         $this->assign('type',$type);
     	 $this->assign('info',$newsList['data'][0]);

         return $this->fetch();      
    }
    
    /**
     * 删除消息
     */
    public function del($newsId = null) {
    	if(IS_AJAX){
    		return $this->api->del($newsId);
    	}
    }
    
    /**
     * 审核申请
     * @param string $id
     */
    public function audit($id = null){
    	return $this->api->audit($id);
    }
    
    
  }



